package core;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

import javax.swing.ImageIcon;

import projectiles.Projectile;

public abstract class ShootingUnit extends Unit {
	
	protected Ellipse2D.Double rangeBounds;
	protected int firerate;
	protected int range;
	protected long nextFire;
	protected boolean nuked;
	protected int nukedCount;
	protected String nukedImage;
	protected boolean fireStorm;
	protected int fireStormTime = (int)(Math.random() * 50);
	protected int fireStormTime2;
	
	public ShootingUnit( Point2D.Double position, int health, ImageIcon image, Dimension size, int viewAngle, int range, int firerate )
	{
		super( position, health, image, size, viewAngle );

		this.firerate = firerate;
		this.range = range;
		
		this.nextFire = System.currentTimeMillis() + firerate;
	}
	
	/**
	 * @return the firerate
	 */
	public int getFirerate() {
		return firerate;
	}
	
	/**
	 * Tijd (in de toekomst) wanneer unit weer mag schieten
	 */
	public void updateFirerate() {
		nextFire = System.currentTimeMillis() + getFirerate();
	}

	/**
	 * @return the range
	 */
	public int getRange() {
		return range;
	}
	
	public boolean canShoot()
	{
		if(nuked || fireStorm)
			return false;
		return ( nextFire <= System.currentTimeMillis() );
	}
	
	public abstract Projectile shoot();
	
	public double getDamageMultiplier()
	{
		return 1;
	}
	
	/*
	 * @see core.Unit#draw(java.awt.Graphics, java.awt.Rectangle, java.awt.Component)
	 */
	public void draw(Graphics g, Rectangle area, Component c )
	{
		if ( isOutsideView() )
			return;
		
		super.draw(g, area, c);
		
		if(!nuked && !fireStorm){
		
			// Shaduw health
			g.setColor( Color.BLACK );
			g.setFont( new Font("Arial", Font.PLAIN, 9) );
			g.drawString( Math.round((getHealth() / (double)getMaxHealth() * 100)) + "%", (int)position.x - 4, (int)position.y + 1);
	
			// Health
			g.setColor( Color.WHITE );
			g.setFont( new Font("Arial", Font.PLAIN, 9) );
			g.drawString( Math.round((getHealth() / (double)getMaxHealth() * 100)) + "%", (int)position.x - 5, (int)position.y);
		}
	}
	
	public void SetNuked(){
		//vijand geraakt door nuke
		nuked = true;
		this.setHealth(0);
		this.image = new ImageIcon(nukedImage);
		
	}
	
	public int getNukedCount(){
		return nukedCount;
	}
	
	public boolean update(){
		if(nuked){
			nukedCount++;
			return true;
		}if(fireStorm){
			fireStormTime2++;
			return true;
		}
		return super.update();
	}
	
	public void moveObjectFireStorm(){
		fireStorm = true;
		this.setHealth(0);
		this.viewAngle = (int)(Math.random() * 360);
	}


}
